from django.test import TestCase
from PIL import Image

def make_thumb(path):
    pixbuf = Image.open(path)
    width,height = pixbuf.size
    print 'width:',width,'  height:',height
    bl = 345.0/270.0
    if height*bl > width:
        img = pixbuf.crop((0,int((height-(width/bl))/2),width,int(height-((height-(width/bl))/2))))
    else:
        img = pixbuf.crop((int((width-(height*bl))/2),0,int(width-((width-(height*bl))/2)),height))
    img.thumbnail((345,270),Image.ANTIALIAS)
    thumb_path = get_thumb_path(path)
    img.save(thumb_path)


def get_thumb_path(path):
    thumb_all_name = path.split('/')
    ext = thumb_all_name[-1].split('.')[-1]
    name = thumb_all_name[-1].split('.')[0]+'_thumb'
    del thumb_all_name[-1]
    thumb_path = '/'.join(thumb_all_name)+'/'+name+'.'+ext
    print thumb_path
    return thumb_path

make_thumb('../picture/picture/2017/03/15/6180470719312436.jpg')


